package main

import (
	"bytes"
	"encoding/csv"
	"fmt"
	"os"
	"sort"

	"github.com/tidwall/gjson"
)

func main() {
	data, err := os.ReadFile("xxxx.txt")
	if err != nil {
		panic(err)
	}

	result := gjson.Get(string(data), "tag").Array()
	heroes := make([]string, 0, len(result))
	for _, item := range result {
		heroes = append(heroes, item.Get("sTag").String())
	}
	fmt.Println(heroes)
	fmt.Println(len(heroes))

	buff := bytes.Buffer{}
	writer := csv.NewWriter(&buff)
	if err := writer.Write([]string{"编号", "英雄名称", "是否使用"}); err != nil {
		panic(err)
	}

	sort.Strings(heroes)
	for i, hero := range heroes {
		if err := writer.Write([]string{fmt.Sprintf("%d", i+1), hero, "1"}); err != nil {
			panic(err)
		}
	}
	writer.Flush()

	if err = os.WriteFile("result.csv", buff.Bytes(), 0644); err != nil {
		panic(err)
	}
}
